/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.faesa.acervo.business.ejb;

import br.faesa.acervo.entity.Emprestimo;
import br.faesa.acervo.entity.Livro;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author Eric Rosetti Lessa
 */
@Stateless
@LocalBean
public class EmprestimoDAO extends AcervoDAO<Emprestimo>{
    
    public EmprestimoDAO(){
        super(Emprestimo.class);
    }
    
    public Long countEmprestimosByIntervalo(Date dtEmprestimo, Date dtDevolucao, Livro livro){
        
        CriteriaBuilder cb =  super.em.getCriteriaBuilder();
        
        CriteriaQuery<Long> cq = cb.createQuery(Long.class);
        
        Root<Emprestimo> r = cq.from(entityClass);
        
        Expression<Date> dtEmpExp = cb.literal(dtEmprestimo);
        Expression<Date> dtDevExp = cb.literal(dtDevolucao);
        Expression<Date> dtDevToCompare = r.get("dtDevolucao");
        Expression<Date> dtEmpToCompare = r.get("dtEmprestimo");
        
        Predicate betParamsDev = cb.between(dtEmpExp, dtEmpToCompare, dtDevToCompare);
        Predicate betDtDev = cb.between(dtEmpToCompare, dtEmpExp, dtDevExp);
        
        Predicate betDtEmp = cb.between(dtDevExp, dtEmpToCompare, dtDevToCompare);
        Predicate betParamsEmp = cb.between(dtDevToCompare, dtEmpExp, dtDevExp);
        
        Predicate or = cb.or(betDtDev, betDtEmp, betParamsDev, betParamsEmp);
        
        cq.where(cb.and(or, cb.equal(r.get("livro"), livro)));
        
        cq.select(cb.count(r));
        
        return super.em.createQuery(cq).getSingleResult();
    }
    
    public List<Emprestimo> findEmprestimosByLivro(Livro livro, boolean historico){
        
        CriteriaBuilder cb = super.em.getCriteriaBuilder();
        
        CriteriaQuery<Emprestimo> cq = cb.createQuery(Emprestimo.class);
        
        Root<Emprestimo> r = cq.from(entityClass);
        
        Predicate equalLivro = cb.equal(r.get("livro"), livro);
        
        Expression<Date> expDtEmp = r.get("dtEmprestimo");
        
        Predicate and ;
        if(historico){
           and = cb.and(equalLivro, cb.lessThanOrEqualTo(expDtEmp, cb.literal(new Date())));
        }else{
           and = cb.and(equalLivro, cb.greaterThan(expDtEmp, cb.literal(new Date())));
        }
        
        cq.where(and);
        
        cq.select(r);
        
        cq.orderBy(cb.asc(r.get("dtEmprestimo")));
        
        return super.em.createQuery(cq).getResultList();
    }
    
}
